﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebNext.Domain.Entities;
using System.Web.Mvc;
using System.Xml.Serialization;
using WebNext.Domain;
using WebNext.UI.Properties;
using WebNext.UI.Utils;

namespace WebNext.UI.Models.Entries
{
	public class EntryHeaderElementViewModel : EntryElementViewModel
	{
		public override EntryElementType Type
		{
			get { return EntryElementType.Header; }
		}

		public string Header
		{
			get;
			private set;
		}

		public bool IsHeaderValid
		{
			get;
			private set;
		}

		public EntryHeaderElementViewModel(EntryHeaderElement entryHeaderElement) : base(entryHeaderElement.Order)
		{
			this.Header = entryHeaderElement.Header;
			this.IsHeaderValid = true;
		}

		public EntryHeaderElementViewModel(int order, IDictionary<string, string> properties) : base(order)
		{
			if (properties.ContainsKey("header"))
			{
				this.Header = properties["header"];
			}
			this.IsHeaderValid = true;
		}

		public override EntryElement GetEntryElement()
		{
			return new EntryHeaderElement { Header = this.Header, Order = this.Order, Type = this.Type };
		}

		public override ValidationResult Validate()
		{
			ValidationResult result = new ValidationResult();
			if (string.IsNullOrWhiteSpace(this.Header))
			{
				result.AddError(Resources.EntryHeaderCannotBeEmpty);
				this.IsHeaderValid = false;
			}
			else if (this.Header.Length > EntryChapterElement.MaxChapterLength)
			{
				result.AddError(Resources.EntryHeaderTooLong, EntryHeaderElement.MaxHeaderLength);
				this.IsHeaderValid = false;
			}
			return result;
		}
	}
}